package com.jbm.jbmsupplier.activity.register.presenter

import android.content.Context
import android.os.CountDownTimer
import android.view.View
import com.jbm.core.common.MainServer
import com.jbm.enterapplication.InterfaceMa
import com.jbm.jbmsupplier.activity.loadhomepage.factoryhome.LoadHomeFactory
import com.jbm.jbmsupplier.activity.register.mode.Regiter_Mode_User_Con
import com.jbm.jbmsupplier.activity.register.view.Register_Construction_Massages
import com.jbm.jbmsupplier.activity.rxjavanet.ModeRxjavaNet
import com.jbm.jbmsupplier.shared.SharedPreferActivity
import com.jbm.jbmsupplier.view.DialogLoading
import com.jbm.jbmsupplier.view.DoubleButtonDialog
import com.jbm.onehome.common.OHInterface
import com.jbm.onehome.iface.Login
import rx.Observable
import rx.Observer

/**
 * Created by 家帮忙 on 2017/8/23.
 */
class Register_Present_Con : Register_Present_Interface_Con {


    var regitermodeusercon = Regiter_Mode_User_Con();
    var register_construction_massagtes: Register_Construction_Massages? = null
    var context:Context?=null
    private var sharedPreferActivity: SharedPreferActivity? = null
     var  loadhomefactory: LoadHomeFactory?=null

    constructor(register_construction_massagtes: Register_Construction_Massages?, context:Context?) {
        this.register_construction_massagtes = register_construction_massagtes
        this.context=context
        sharedPreferActivity = SharedPreferActivity(context, true)
         loadhomefactory = LoadHomeFactory()
        dialogLoading = DialogLoading(context!!, "加载中.....", true)
    }
    override fun getRegisterUserMassage(content: Context) {//注册施工段
        if (register_construction_massagtes!!.getPhone().equals("")){
            register_construction_massagtes!!.getPhoneErr().error = "手机号不能为空"
            return
        }else if(register_construction_massagtes!!.getPhone().length<11){
            register_construction_massagtes!!.getPhoneErr().error = "手机号长度不符"
            return
        }else if(register_construction_massagtes!!.getValidateCode().equals("")){
            register_construction_massagtes!!.getValidateCodeErr().error = "验证码不能为空"
            return
        }else if(register_construction_massagtes!!.getPasWord().equals("")){
            register_construction_massagtes!!.getPasWordErr().error = "密码不能为空"
            return
        }else if(register_construction_massagtes!!.getPasWordzc().equals("")){
            register_construction_massagtes!!.getPasWordErrzc().error = "密码不能为空"
            return
        }else if(!register_construction_massagtes!!.getPasWord().equals(register_construction_massagtes!!.getPasWordzc())){
            register_construction_massagtes!!.getPasWordErr().error = "两次密码不一致"
            return
        } else{

            dialogLoading?.show()
        regitermodeusercon.GainRegisterIntergace(register_construction_massagtes!!.getPhone(), register_construction_massagtes!!.getValidateCode(), "",loginid!!,object :InterfaceMa<String>{
            override fun onSuccess(t: String, boolean: Boolean) {
                setDialogding()
                ModeRxjavaNet.getInstantMode().objects(Observable.create(Observable.OnSubscribe<Login> { obVersion ->
                    obVersion.onNext(  ModeRxjavaNet.getInstantMode().getOhInter().RegistCustomer(Integer.parseInt(t), "",  register_construction_massagtes!!.getPasWord(),  register_construction_massagtes!!.getPasWordzc(), "", false, ""))
                })).subscribe(object : Observer<Login> {
                    override fun onNext(t: Login?) {
                        if (t?.m_code == 200) {
                            setDialogding()
                            sharedPreferActivity?.setName(register_construction_massagtes!!.getPhone(), register_construction_massagtes!!.getPasWord())
                            sharedPreferActivity?.setBoolearLogin(true)//判断是否登陆
                            sharedPreferActivity?.setBoolearNetConn(true)//判断是否第一次连接服务器，如果登录成功就设为true 代表第一次链接成功
                            sharedPreferActivity?.setLoginid(OHInterface.getG_server().login_id)
                            sharedPreferActivity?.setUserName(OHInterface.getG_server().nickname)
                            MainServer.getG_server().m_local_id = OHInterface.getG_server().login_id
                            loadhomefactory!!.getLoagHomePage("2").LoadNewHome(context!!)//第二个代表管理端
//                            ModeRxjavaNet.getInstantMode().objects(Observable.create<PrivilegeListEntity> {
//                                subscriber ->
//                                subscriber.onNext(ModeRxjavaNet.getInstantMode().getOhInter().getPersonalPrivilegeList(true, 0, OHInterface.getG_server().login_id)) }).subscribe(object : Observer<PrivilegeListEntity> {
//                                override fun onCompleted() {
//
//                                }
//
//                                override fun onError(e: Throwable) {
//
//                                }
//
//                                override fun onNext(privilegeListEntity: PrivilegeListEntity) {
//                                    if (privilegeListEntity.m_code == 200) {
//                                        val arrPteson = ArrayList<PrivilegeListEntity>()
//                                        arrPteson.addAll(privilegeListEntity.priList)
//                                        PtesonalPrivile().setArrPteson(arrPteson, context)//将所有的权限保存在本地
//
//
//                                    } else {
//                                    }
//
//                                }
//                            })
                        } else if (t?.m_code == 500) {
                            //密码设置验证失败
                            setDialogding()
                            hint(t!!.m_info)
                        } else {
                            //密码设置验证失败
                            setDialogding()
                            hint(t!!.m_info)
                        }
                    }

                    override fun onError(e: Throwable?) {




                    }

                    override fun onCompleted() {
                    }

                })







            }

            override fun setError(err: String, boolean: Boolean) {
                hint(err)
            }

        })
    }}
    var  mcount: MyCount?=null
    var  dialogLoading: DialogLoading?=null
    var loginid:String?=null
    override fun getValidateCode() {//获取验证码
        if(register_construction_massagtes!!.getPhone().equals("")){
            register_construction_massagtes!!.getPhoneErr().error = "手机号不能为空"
            return
        }else if(register_construction_massagtes!!.getPhone().length<11){
            register_construction_massagtes!!.getPhoneErr().error = "手机号长度不符"
            return
        }else{
            mcount = MyCount(90000, 1000)
            mcount?.start()
            dialogLoading = DialogLoading(context!!, "加载中.....", false)
            dialogLoading?.show()
        regitermodeusercon.GainValidateCode(register_construction_massagtes!!.getPhone(),object :InterfaceMa<String>{
            override fun onSuccess(t: String, boolean: Boolean) {
                if (boolean==true){
                    setDialogding()
                    loginid = t
                }
            }
            override fun setError(err: String, boolean: Boolean) {
                if (boolean==false){
                    setDialogding()
                    hint(err)
                    if (mcount != null) {
                        register_construction_massagtes?.getTextView()?.text="发送验证码"
                        mcount?.cancel()
                    }
                }
            }

        })
        }
    }

    //一些提示的时候调用
    fun hint(name: String) {
        val dialog = DoubleButtonDialog(context, name)
        dialog.getokbtn().setOnClickListener(View.OnClickListener { dialog.dismiss() })
        dialog.setisVisibility()

    }
    fun setDialogding() {
        if (dialogLoading != null) {
            dialogLoading!!.dismiss()
        }
    }

    /*定义一个倒计时的内部类*/
    inner class MyCount(millisInFuture: Long, countDownInterval: Long) : CountDownTimer(millisInFuture, countDownInterval) {

        override fun onFinish() {
            register_construction_massagtes?.getTextView()?.text="发送验证码"

        }

        override fun onTick(millisUntilFinished: Long) {
            register_construction_massagtes?.getTextView()?.text="请等待90秒(" + millisUntilFinished / 1000 + ")..."

        }
    }
}